package com.minew.beaconset.dobackground;

import com.minew.beaconset.MinewBeacon;
import com.minew.beaconset.App;
import com.minew.beaconset.beaconset.comparator.MapValueComparator;
import com.minew.beaconset.bean.LocaltionLog;
import com.minew.beaconset.bean.MinewBeaconTimes;
import com.minew.beaconset.db.DbHelper;
import com.minew.beaconset.task.Task;
import com.minew.beaconset.utils.CommonUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.UUID;

/**
 * 保存数据
 */
public class SaveLocalDataTask extends Task {
    @Override
    public Task doTask() {
         Map<String, Map<Float, MinewBeaconTimes>> temp = getParam("temp");
         Map<String,MinewBeacon> beacons =getParam("beacons");
         int minRssi = getParam("minRssi");
         int maxRssi = getParam("maxRssi");
         try {
             List<MinewBeacon> leaves = new ArrayList<>();
             List<MinewBeacon> appears = new ArrayList<>();
             for(String mac:beacons.keySet()){
                 if(temp.get(mac)==null){
                     if(minRssi!=0&&maxRssi!=0){
                         if(beacons.get(mac).getRssi()>=minRssi
                                 &&beacons.get(mac).getRssi()<maxRssi){
                             leaves.add(beacons.get(mac));
                             beacons.remove(mac);
                         }
                     }else{
                         leaves.add(beacons.get(mac));
                         beacons.remove(mac);
                     }
                 }
             }
             List<MinewBeacon> activies = new ArrayList<>();
             for(String mac:temp.keySet()){
                 Map<Float, MinewBeaconTimes> map = temp.get(mac);
                 MapValueComparator bvc = new MapValueComparator(map);
                 TreeMap<Float, MinewBeaconTimes> sorted_map = new TreeMap<>(bvc);
                 sorted_map.putAll(map);
                 Set keys = map.keySet();
                 MinewBeacon t = null;
                 if(keys.iterator().hasNext()){
                     Float f = (Float)keys.iterator().next();
                     t = map.get(f).getMinewBeacon();
                 }
                 if(beacons.get(mac)==null){
                     if(t!=null) {
                         if(minRssi!=0&&maxRssi!=0){
                             if(t.getRssi()>=minRssi&&
                                     t.getRssi()<maxRssi){
                                 beacons.put(mac, t);
                                 appears.add(t);
                             }
                         }else{
                             beacons.put(mac, t);
                             appears.add(t);
                         }
                     }
                 }else{
                     if(t!=null) {
                         if(minRssi!=0){
                             if(t.getRssi()>=minRssi){
                                 activies.add(t);
                             }
                         }else{
                             activies.add(t);
                         }

                     }
                 }
             }
             List<LocaltionLog> localtionLogs = new ArrayList<>();
             //离开的设备
             if(leaves.size()>0){
                 for(MinewBeacon beacon:leaves){
                     LocaltionLog localtionLog = new LocaltionLog();
                     localtionLog.setId(UUID.randomUUID().toString());
                     localtionLog.setName(beacon.getName());
                     localtionLog.setBattery(beacon.getBattery());
                     localtionLog.setDistance(beacon.getDistance());
                     localtionLog.setMac(beacon.getMacAddress());
                     localtionLog.setCreateDate(new Date(beacon.getAddTime()));
                     localtionLog.setType(LocaltionLog.TYPE_OUT);
                     localtionLogs.add(localtionLog);
                 }
             }
             //出现的设备
             if(appears.size()>0){
                 for(MinewBeacon beacon:appears){
                     LocaltionLog localtionLog = new LocaltionLog();
                     localtionLog.setId(UUID.randomUUID().toString());
                     localtionLog.setName(beacon.getName());
                     localtionLog.setBattery(beacon.getBattery());
                     localtionLog.setDistance(beacon.getDistance());
                     localtionLog.setMac(beacon.getMacAddress());
                     localtionLog.setCreateDate(new Date(beacon.getAddTime()));
                     localtionLog.setType(LocaltionLog.TYPE_IN);
                     localtionLogs.add(localtionLog);
                 }
             }
             //当前活跃的设备
             if(activies.size()>0){
                 for(MinewBeacon beacon:activies){
                     LocaltionLog localtionLog = new LocaltionLog();
                     localtionLog.setId(UUID.randomUUID().toString());
                     localtionLog.setName(beacon.getName());
                     localtionLog.setBattery(beacon.getBattery());
                     localtionLog.setDistance(beacon.getDistance());
                     localtionLog.setMac(beacon.getMacAddress());
                     localtionLog.setCreateDate(new Date(beacon.getAddTime()));
                     localtionLog.setType(LocaltionLog.TYPE_ACTIVITY);
                     localtionLogs.add(localtionLog);
                 }
             }
             DbHelper.save(localtionLogs);
             //进去识别区
             if(appears.size()>0&&leaves.size()==0&&activies.size()==0){
                 CommonUtils.vibrator(App.getApp(),true);
             }
             //离开识别区
             if(leaves.size()>0&&appears.size()==0&&activies.size()==0){
                 CommonUtils.vibrator(App.getApp(),false);
             }
         }catch (Exception e){
             e.printStackTrace();
         }
        set("beacons",beacons);
        return this;
    }
}
